/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : kf_ic.c                                               *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Main Interrupt Service Routines.                      *      
 *                      This file provides template for all exceptions        *
 *                      handler and peripherals interrupt service routine.    *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                          Include Files                                    **
******************************************************************************/
#include "system_init.h"
#include "SPI.h"
#include <stdio.h>
/*******************************************************************************
**                   KF32A156 Processor Exceptions Handlers  		         **
*******************************************************************************/
extern void delay_ms(volatile uint32_t nms);
extern uint8_t Spi_Finish_Flag;
uint8_t Spi_Change_Flag = 0;
 uint32_t counter_int=0;
//*****************************************************************************************
//                                 NMI Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _NMI_exception(void)
{
}

//*****************************************************************************************
//                               HardFault Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _HardFault_exception(void)
{
}

//*****************************************************************************************
//                               StackFault Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _StackFault_exception(void)
{
}

//*****************************************************************************************
//                               SVC Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SVC_exception(void)
{
}

//*****************************************************************************************
//                              SoftSV Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SoftSV_exception(void)
{
}

//*****************************************************************************************
//                              SysTick Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SysTick_exception(void)
{
}

//*****************************************************************************************
//                              SysTick Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SPI0_exception(void)
{
}

//*****************************************************************************************
//                              DMA0 Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _DMA0_exception(void)
{

	counter_int++;
	if(counter_int>=100)
	{
		/* led reversal */
		GPIO_Toggle_Output_Data_Config(GPIOC_SFR, GPIO_PIN_MASK_5);
		counter_int =0;
	}
	/* Wait DMA receive data complete */
	if (DMA_Get_Finish_Transfer_INT_Flag(DMA0_SFR, DMA_CHANNEL_2))
	{
		/* Outgoing data received by Master */
		fprintf(USART4_STREAM,"%s\r\n", Spi_ReceiveData);
		/* Clear DMA receive complete flag */
		DMA_Clear_INT_Flag(DMA0_SFR, DMA_CHANNEL_2, DMA_INT_FINISH_TRANSFER);

	}

	if (DMA_Get_Finish_Transfer_INT_Flag(DMA0_SFR, DMA_CHANNEL_1))
	{
		/* After the DMA finishes sending for the first time, set the number of data to be
		 * transferred and the starting memory address of the DMA channel to the actual size 
		 * and starting position of the string sent  */
		if (Spi_Change_Flag == 0)
		{
			/* Transmit data size */
			DMA_Transfer_Number_Config(DMA0_SFR, DMA_CHANNEL_1, SPI_TRANSMISSION_DATA_SIZE);
			/* Memory start address */
			DMA_Memory_Start_Address_Config(DMA0_SFR, DMA_CHANNEL_1, (uint32_t)&Tx_Master);
			Spi_Change_Flag = 1;
		}
		/* Waiting for spi to send no data */
		while (SPI_Get_BUSY_Flag(SPI0_SFR) == SET)
			;
		/* Clear interrupt flag */
		DMA_Clear_INT_Flag(DMA0_SFR, DMA_CHANNEL_1, DMA_INT_FINISH_TRANSFER);
		/* Enable next transmit */
		DMA_Channel_Enable(DMA0_SFR, DMA_CHANNEL_1, TRUE);
		delay_ms(10);
	}
}
